﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace Project01
{
    class MonsterType02:Monster
    {
        //Dữ liệu về Monster02
        private byte couter=0;
        private bool createbullets = false;
        private TimeSpan TimeRemoveBullets;
        public MonsterType02(Game game, int Position)
            : base(game,Position)
        {
                this.ObjectTexture = game.Content.Load<Texture2D>("Images/Monster/Mtype02");
                this.Sprite = new AnimationSprite(ObjectTexture, 1, 1);
                this.Speed = 3;
                this.Sprite.Position = new Vector2(Ran.Next(0, game.Window.ClientBounds.Width)*Position, 0 - Sprite.RecHeight);
                this.Health = 40;
                this.game = game;
              
               
              
        }
        public override void SetPositionBullets(Vector2 Position)
        {
            for (int i = 0; i < 6; i++)
            {
                Bullets bullets = new Bullets(game, "Images/Bullets/Monster/B_M_Type02", 1, 1);
                bullets.Sprite.Scale = 2f;
                bullets.Speed = 10;
                Mbullets.Add(bullets);
            }

            Mbullets[0].Sprite.Position = new Vector2(Position.X+15, Position.Y+60);
            Mbullets[1].Sprite.Position = new Vector2(Position.X + 30, Position.Y + 70);
            Mbullets[2].Sprite.Position = new Vector2(Position.X +  45, Position.Y + 60);
            Mbullets[3].Sprite.Position = new Vector2(Position.X + 130, Position.Y + 60);
            Mbullets[4].Sprite.Position = new Vector2(Position.X +  145, Position.Y + 70);
            Mbullets[5].Sprite.Position = new Vector2(Position.X + 160, Position.Y + 60);
            
                base.SetPositionBullets(Position);
        }
        public override void Update(GameTime gameTime)
        {
            this.Sprite.Update();

            if (this.Sprite.Position.Y <= game.Window.ClientBounds.Height / 10 )
            {
                this.Sprite.Position.Y += Speed;
            }
            else

            {
                if (!createbullets)
                {
                    SetPositionBullets(this.Sprite.Position);
                    createbullets = true;
                }
                UpdateBullets(gameTime);
            }
            if (MoveState == MonsterMove.Left)
            {
                this.Sprite.Position.X += Speed;
            }
            else
            {
                this.Sprite.Position.X -= Speed;
            }

           
            if (this.Sprite.Position.X <= 0)
                MoveState = MonsterMove.Left;
            else if(this.Sprite.Position.X>=game.Window.ClientBounds.Width-this.Sprite.RecWidth)
                MoveState = MonsterMove.Right ;


            base.Update(gameTime);
        }
        private void UpdateBullets( GameTime gameTime)
        {
            TimeRemoveBullets += gameTime.ElapsedGameTime;
            if (TimeRemoveBullets >= TimeSpan.FromSeconds(1.2))
            {
                TimeRemoveBullets = TimeSpan.Zero;
                createbullets = false;
            }
            for (int i = 0; i < Mbullets.Count; i++)
            {

                if (couter >= 3) couter = 0;
                switch (couter)
                {
                    case 0:
                        {
                            Mbullets[i].Sprite.Position.X -= Mbullets[i].Speed;
                            Mbullets[i].Sprite.Position.Y += Mbullets[i].Speed;
                            break;
                        }
                    case 1:
                        Mbullets[i].Sprite.Position.Y += Mbullets[i].Speed;
                        break;
                    case 2:
                        {
                            Mbullets[i].Sprite.Position.X += Mbullets[i].Speed;
                            Mbullets[i].Sprite.Position.Y += Mbullets[i].Speed;
                            break;
                        }
                }
                couter++;
                Mbullets[i].Sprite.Update();

                if (Mbullets[i].Sprite.Position.Y >= game.Window.ClientBounds.Height+Mbullets[i].Sprite.RecHeight)
                    Mbullets.RemoveAt(i);
             
            }
           
        }
        public override void Draw(SpriteBatch SpriteBatch)
        {
            this.Sprite.Draw(SpriteBatch);
            for (int i = 0; i < Mbullets.Count; i++)
            {
                Mbullets[i].Draw(SpriteBatch);
            }
            base.Draw(SpriteBatch);
        }
   
    }
}
